package com.zhuxin.medical.listener;

import com.dbHelper.BeanUtil;
import com.dbHelper.DBUtil;
import com.dbHelper.DbTemplate;
import com.dbHelper.SqlCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * Created by lzb on 2018/9/18.
 */

@WebListener
public class StartListener implements ServletContextListener {

    static Logger logger = LoggerFactory.getLogger(StartListener.class);

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println(logger.getClass().getName());
        long time=System.currentTimeMillis();
        ServletContext context=sce.getServletContext();
        DBUtil.ServerAbsolutePath=context.getRealPath("/");
        DBUtil.ContextPath=context.getContextPath();
        try {
            DBUtil.classMap= BeanUtil.getClassResource(DBUtil.getClasssFromPackage("com.zhuxin.medical.model"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        SqlCache.init("com.zhuxin.medical.sql");
        final WebApplicationContext ctx= WebApplicationContextUtils.getWebApplicationContext(context);
        logger.info("系统初始化完成!"+"已加载"+ DBUtil.classMap.size()+"个类对象,网站根目录为:'"+ DBUtil.ServerAbsolutePath+"',ContextPath:'"+ DBUtil.ContextPath+"',共耗时:"+(System.currentTimeMillis()-time)+"ms!");

    }

}
